<template>
	<view class="content">
	
		<view class="">{{title}}</view>
		<view class="">温度：{{temp}}</view>
		<view class="">湿度 ：{{humi}}</view>
		<switch :checked="led" @change="onLedSwitch"></switch>
		
	</view>
</template>

<script>
	const {
		createCommonToken
	} = require('@/key.js')
	export default {
		data() {
			return {
				temp:'',
				humi:'',
				led:"",
				title: 'Hello',
				token:""
			}
		},
		onLoad() {
			const params = {
				author_key: "OzWcPbNJab0HGeipMVU/L7Znxfjt2t6fAXlZsB/3b/LhrHzUVGI1d+78XHf12cgmyYEuY1k+2r69v5XJ7lJY4A==",
				version: "2022-05-01",
				user_id: "261462",
			}
			this.token = createCommonToken(params);

		},
		onShow(){
			this.fetchData();
		},
		methods: {
			fetchData(){
				uni.request({
					  url: 'https://iot-api.heclouds.com/thingmodel/query-device-property',
					  method:"GET",
					  data: {
						  product_id:"Y4l9I4hhmh",
						  device_name:'d1',
					  },
					  header: {
					    'authorization': this.token // 默认值
					  },
					 //  success (res) {  这样写不行，明显的错误
					 //    console.log(res.data);
						// this.temp =res.data.data[2].value;
					 //    console.log(res.data.data[2].value);
						// this.humi = res.data.data[0].value;
						// console.log(res.data.data[0].value);
						// this.led =res.data.data[1].value === true;
						// console.log(this.led);
					 //  }
					  success :(res) =>{
							// console.log(res.data);
							this.temp =res.data.data[2].value;
							// console.log(res.data.data[2].value);
							this.humi = res.data.data[0].value;
							// console.log(res.data.data[0].value);
							this.led =res.data.data[1].value === true;
							// console.log(this.led);
					  }
					  
				})
				
				
			},
			onLedSwitch(event){
				// let value = console.log(event); //获取值的状态
				
				 console.log(event.detail.value); //获取值的状态
				// let value = console.log(event.detail.value); //获取值的状态  //！！！逆天的代码  2024年3月7日
				let value = event.detail.value;
				uni.request({
					  url: 'https://iot-api.heclouds.com/thingmodel/set-device-desired-property', 
					  method:"POST",
					  data: {
						  product_id:"Y4l9I4hhmh",
						  device_name:'d1',
						  params: { "led":value }
					  },
					 header: {
					  'authorization': this.token // 默认值
					},
					  success :() =>{
						 console.log("LED-"+ ( value ==  true ? "ON":"OFF"));
					  }
					  
				})
				
				
			}

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>